package cn.itcast.service.impl;

import cn.itcast.service.OrderSettingService;
import cn.itcast.dao.OrderSettingDao;
import cn.itcast.pojo.OrderSetting;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 设置预约服务
 */
@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {

    @Autowired
    private OrderSettingDao orderSettingDao;

    /***
     * 批量添加
     *
     */
    @Override
    public void add(List<OrderSetting> list) {

        //批量导入预约人数
        if (list != null && list.size() > 0) {
            for (OrderSetting orderSetting : list) {
//                Date orderDate = orderSetting.getOrderDate();
//                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-hh");
//                String format = simpleDateFormat.format(orderDate);
//                System.out.println(format+"-----------");
                //判断当前日期是否已经进行了预约设置
                Date orderDate = orderSetting.getOrderDate();
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                String date = format.format(orderDate);
                long countByOrderDate = orderSettingDao.findCountByOrderDate(date);
                if (countByOrderDate > 0) {
                    //已经进行了预约设置,执行更新操作
                    orderSettingDao.editNumberByOrderDate(orderSetting, date);
                } else {
                    //没有进行预约设置,执行插入操作
                    orderSettingDao.add(orderSetting);
                }
            }
        }
    }


    /**
     * 根据日期查询预约的设置数据（获取指定的日期所在的月份预约设置数据）
     *
     * @param date
     * @return
     */

    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        //拼接
        String dateBegin = date + "-01";//2019-3-1
        String dateEnd = date + "-31";//2019-3-31
        //定义一个Map
        Map<String, String> map = new HashMap<>();
        //添加
        map.put("dateBegin", dateBegin);
        map.put("dateEnd", dateEnd);
        //调用
        // List<OrderSetting> list得到的结果
        List<OrderSetting> list = orderSettingDao.getOrderSettingByMonth(map);
        //最终返回的结果的对象
        List<Map> data = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (OrderSetting orderSetting : list) {
                Map<String, Object> orderSettingMap = new HashMap<>();
                //获得日期几号
                //getDate获取当前几号
                orderSettingMap.put("date", orderSetting.getOrderDate().getDate());
                //可以预约的人数
                orderSettingMap.put("number", orderSetting.getNumber());
                //已近预约的人数
                orderSettingMap.put("reservations", orderSetting.getReservations());
                //存储到data中
                data.add(orderSettingMap);
            }
        }
        return data;

    }

    @Override
    public void editNumberByDate(OrderSetting orderSetting) {
//查询当前日期是否进行了预约设置
        //根据日期查询是否已经进行了预约设置
        Date orderDate = orderSetting.getOrderDate();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String date = format.format(orderDate);
        long count = orderSettingDao.findCountByOrderDate(date);
        if (count > 0) {
            orderSettingDao.editNumberByOrderDate(orderSetting, date);
        } else {
            //当前日期未进行预约设置,需要进行插入操作
            orderSettingDao.add(orderSetting);
        }
    }
}
